package chapterSystem.client.interfaceDrawing;

import chapterSystem.client.ChapterSystem;
import chapterSystem.client.ChapterSystemService;
import chapterSystem.client.model.entities.BrotherEntity;
import chapterSystem.client.model.entitiesInt.Brother;
import chapterSystem.client.interfaceDrawing.activities.*;
import chapterSystem.client.model.entitiesInt.Report;
import chapterSystem.client.model.enums.ActivityType;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

import java.util.Arrays;
import java.util.List;

/**
 * User: Hasp
 * Date: 10.03.12
 * Time: 11:38
 */
public class ControlsDrawer {
    private static List<String> menuList = Arrays.asList("Main", "My Orders", "My Reports", "My Unit",
            "Events", "Add new brother");

    private static String LABEL_COMMAND = "<-///////->";

    private ControlsDrawer(RootLayoutPanel rootPanel) {
        this.rootPanel = rootPanel;
    }

    final private RootLayoutPanel rootPanel;
    final private DockLayoutPanel mainPanel = new DockLayoutPanel(Style.Unit.EM);
    final private SplitLayoutPanel leftElementBase = new SplitLayoutPanel();
    final private FlexTable menuTable = new FlexTable();
    final private VerticalPanel footer = new VerticalPanel();
    final private HTML header = new HTML("<h1 style=\"color:#ffffff\">Chapter System</h1>");
    final private StackLayoutPanel workArea = new StackLayoutPanel(Style.Unit.EM);

    private MainActivity mainActivity;
    private ReportsActivity reportsActivity;
    private OrdersActivity ordersActivity;
    private ChapterEventsActivity eventsActivity;
    private AddNewBrotherActivity addNewBrotherActivity;
    private UnitActivity unitActivity;

    private String name;
    private int rankBattle;
    private int rankWork;
    private int points;
    private int id;
    private int unitId;

    private void init() {
        for (int i = 0; i < menuList.size(); i++) {
            final String title = menuList.get(i);
            final Button menuButton = InterfaceProvider.getInstance().createMenuButton(title);
            if (title.equals("Main")) {
                menuButton.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        drawMain();
                    }
                });
            } else if (title.equals("My Reports")) {
                menuButton.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        drawReports();
                    }
                });
            } else if (title.equals("My Orders")) {
                menuButton.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        drawOrders();
                    }
                });
            } else if (title.equals("Logout")) {
                menuButton.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        Cookies.removeCookie(ChapterSystem.COOKIE_NAME);
                        Window.Location.reload();
                    }
                });
            } else if (title.equals("Events")) {
                menuButton.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        drawEvents();
                    }
                });
            } else if (title.equals("My Unit")) {
                menuButton.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(final ClickEvent event) {
                        drawUnits();
                    }
                });
            } else if (title.equals("Add new brother")) {
                if (rankBattle == 0 && rankWork == 0) {
                    menuButton.setEnabled(false);
                }

                menuButton.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        drawAddNewBrother();
                    }
                });
            }

            menuButton.setStyleName("button_style");
            menuTable.setWidget(i, 0, menuButton);
        }

        final Label splitLabel = new Label(LABEL_COMMAND);
        splitLabel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
        splitLabel.setStyleName("white_at_black");
        menuTable.setWidget(menuList.size(), 0, splitLabel);

        final Button logoutButton = InterfaceProvider.getInstance().createMenuButton("Logout");
        logoutButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                Cookies.removeCookie(ChapterSystem.COOKIE_NAME);
                Window.Location.reload();
            }
        });
        menuTable.setWidget(menuList.size() + 1, 0, logoutButton);

        menuTable.setBorderWidth(2);
        leftElementBase.setStyleName("menu_background");
        leftElementBase.add(menuTable);

        HTML label = new HTML("_");
        label.addStyleName("withDarkGreyBackground");

        mainPanel.setStyleName("body_style");
        header.setStyleName("body_style");
        footer.setStylePrimaryName("body_style");
        mainPanel.addNorth(header, 5);
        mainPanel.addSouth(footer, 2);

        mainPanel.addWest(leftElementBase, 14.5);
        mainPanel.add(workArea);

        rootPanel.setStyleName("body_style");
        rootPanel.add(mainPanel);

        drawMain();
    }

    private void drawUnits() {
        unitActivity.drawActivity(workArea);
    }

    public void drawMain() {
        mainActivity.drawActivity(workArea);
    }

    public void drawReports() {
//        reportsActivity.drawActivity(workArea);
        ChapterSystemService.App.getInstance().getReportsById(id, new AsyncCallback<List<Report>>() {
            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            @Override
            public void onSuccess(List<Report> result) {
                new SeeReportsActivity(new BrotherEntity(id, unitId, rankBattle, name, rankWork, points), ActivityType.REPORT,
                        "See my reports", result, mainActivity).drawActivity(workArea);
            }
        });
    }

    public void drawOrders() {
        ordersActivity.drawActivity(workArea);
    }

    public void drawEvents() {
        eventsActivity.drawActivity(workArea);
    }

    public void drawAddNewBrother() {
        addNewBrotherActivity.drawActivity(workArea);
    }

    public static class ControlsDrawerBuilder {
        private ControlsDrawer instance;

        public ControlsDrawerBuilder(RootLayoutPanel rootLayoutPanel) {
            instance = new ControlsDrawer(rootLayoutPanel);
        }

        public ControlsDrawerBuilder setName(String name) {
            instance.name = name;
            return this;
        }

        public ControlsDrawerBuilder setRankBattle(int rank) {
            if (rank < 0) {
                throw new IllegalArgumentException();
            }

            instance.rankBattle = rank;
            return this;
        }

        public ControlsDrawerBuilder setRankWork(int rank) {
            if (rank < 0) {
                throw new IllegalArgumentException();
            }

            instance.rankWork = rank;
            return this;
        }

        public ControlsDrawerBuilder setPoints(int points) {
            instance.points = points;
            return this;
        }

        public ControlsDrawerBuilder setId(int id) {
            instance.id = id;
            return this;
        }

        public ControlsDrawerBuilder setUnitId(int unitId) {
            instance.unitId = unitId;
            return this;
        }

        public ControlsDrawer build() {
            final Brother brother = new BrotherEntity(instance.id, 1, instance.rankBattle, instance.name, instance.rankWork, instance.points);
            instance.mainActivity = new MainActivity(brother);
            instance.reportsActivity = new ReportsActivity(brother);
            instance.ordersActivity = new OrdersActivity(brother);
            instance.eventsActivity = new ChapterEventsActivity(brother);
            instance.addNewBrotherActivity = new AddNewBrotherActivity(brother, instance.mainActivity);
            instance.unitActivity = new UnitActivity(brother);
            instance.init();

            return instance;
        }
    }
}
